﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Pharmacy.Entities;
using Pharmacy.Data;

#endregion

namespace Pharmacy.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="AdjustmentDetailsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class AdjustmentDetailsProviderBaseCore : EntityProviderBase<Pharmacy.Entities.AdjustmentDetails, Pharmacy.Entities.AdjustmentDetailsKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Pharmacy.Entities.AdjustmentDetailsKey key)
		{
			return Delete(transactionManager, key.StockOutDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_stockOutDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int64 _stockOutDetailId)
		{
			return Delete(null, _stockOutDetailId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_stockOutDetailId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int64 _stockOutDetailId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		FK_AdjustmentDetails_Adjustment Description: 
		/// </summary>
		/// <param name="_adjustmentId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByAdjustmentId(System.String _adjustmentId)
		{
			int count = -1;
			return GetByAdjustmentId(_adjustmentId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		FK_AdjustmentDetails_Adjustment Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_adjustmentId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		/// <remarks></remarks>
		public TList<AdjustmentDetails> GetByAdjustmentId(TransactionManager transactionManager, System.String _adjustmentId)
		{
			int count = -1;
			return GetByAdjustmentId(transactionManager, _adjustmentId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		FK_AdjustmentDetails_Adjustment Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_adjustmentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByAdjustmentId(TransactionManager transactionManager, System.String _adjustmentId, int start, int pageLength)
		{
			int count = -1;
			return GetByAdjustmentId(transactionManager, _adjustmentId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		fkAdjustmentDetailsAdjustment Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_adjustmentId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByAdjustmentId(System.String _adjustmentId, int start, int pageLength)
		{
			int count =  -1;
			return GetByAdjustmentId(null, _adjustmentId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		fkAdjustmentDetailsAdjustment Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_adjustmentId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByAdjustmentId(System.String _adjustmentId, int start, int pageLength,out int count)
		{
			return GetByAdjustmentId(null, _adjustmentId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_Adjustment key.
		///		FK_AdjustmentDetails_Adjustment Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_adjustmentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public abstract TList<AdjustmentDetails> GetByAdjustmentId(TransactionManager transactionManager, System.String _adjustmentId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		FK_AdjustmentDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByDrugId(System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(_drugId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		FK_AdjustmentDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		/// <remarks></remarks>
		public TList<AdjustmentDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		FK_AdjustmentDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength)
		{
			int count = -1;
			return GetByDrugId(transactionManager, _drugId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		fkAdjustmentDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByDrugId(System.String _drugId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDrugId(null, _drugId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		fkAdjustmentDetailsDrugDispo Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_drugId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public TList<AdjustmentDetails> GetByDrugId(System.String _drugId, int start, int pageLength,out int count)
		{
			return GetByDrugId(null, _drugId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AdjustmentDetails_DrugDispo key.
		///		FK_AdjustmentDetails_DrugDispo Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_drugId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Pharmacy.Entities.AdjustmentDetails objects.</returns>
		public abstract TList<AdjustmentDetails> GetByDrugId(TransactionManager transactionManager, System.String _drugId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Pharmacy.Entities.AdjustmentDetails Get(TransactionManager transactionManager, Pharmacy.Entities.AdjustmentDetailsKey key, int start, int pageLength)
		{
			return GetByStockOutDetailId(transactionManager, key.StockOutDetailId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="_stockOutDetailId"></param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(System.Int64 _stockOutDetailId)
		{
			int count = -1;
			return GetByStockOutDetailId(null,_stockOutDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="_stockOutDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(System.Int64 _stockOutDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByStockOutDetailId(null, _stockOutDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_stockOutDetailId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(TransactionManager transactionManager, System.Int64 _stockOutDetailId)
		{
			int count = -1;
			return GetByStockOutDetailId(transactionManager, _stockOutDetailId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_stockOutDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(TransactionManager transactionManager, System.Int64 _stockOutDetailId, int start, int pageLength)
		{
			int count = -1;
			return GetByStockOutDetailId(transactionManager, _stockOutDetailId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="_stockOutDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(System.Int64 _stockOutDetailId, int start, int pageLength, out int count)
		{
			return GetByStockOutDetailId(null, _stockOutDetailId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AdjustmentDetails index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_stockOutDetailId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Pharmacy.Entities.AdjustmentDetails"/> class.</returns>
		public abstract Pharmacy.Entities.AdjustmentDetails GetByStockOutDetailId(TransactionManager transactionManager, System.Int64 _stockOutDetailId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;AdjustmentDetails&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;AdjustmentDetails&gt;"/></returns>
		public static TList<AdjustmentDetails> Fill(IDataReader reader, TList<AdjustmentDetails> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				Pharmacy.Entities.AdjustmentDetails c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("AdjustmentDetails")
					.Append("|").Append((System.Int64)reader[((int)AdjustmentDetailsColumn.StockOutDetailId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<AdjustmentDetails>(
					key.ToString(), // EntityTrackingKey
					"AdjustmentDetails",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new Pharmacy.Entities.AdjustmentDetails();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.StockOutDetailId = (System.Int64)reader[((int)AdjustmentDetailsColumn.StockOutDetailId - 1)];
					c.AdjustmentId = (System.String)reader[((int)AdjustmentDetailsColumn.AdjustmentId - 1)];
					c.DrugId = (System.String)reader[((int)AdjustmentDetailsColumn.DrugId - 1)];
					c.Quantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.Quantity - 1)];
					c.AdjustQuantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.AdjustQuantity - 1)];
					c.FinalQuantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.FinalQuantity - 1)];
					c.Remark = (reader.IsDBNull(((int)AdjustmentDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)AdjustmentDetailsColumn.Remark - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.AdjustmentDetails"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.AdjustmentDetails"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Pharmacy.Entities.AdjustmentDetails entity)
		{
			if (!reader.Read()) return;
			
			entity.StockOutDetailId = (System.Int64)reader[((int)AdjustmentDetailsColumn.StockOutDetailId - 1)];
			entity.AdjustmentId = (System.String)reader[((int)AdjustmentDetailsColumn.AdjustmentId - 1)];
			entity.DrugId = (System.String)reader[((int)AdjustmentDetailsColumn.DrugId - 1)];
			entity.Quantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.Quantity - 1)];
			entity.AdjustQuantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.AdjustQuantity - 1)];
			entity.FinalQuantity = (System.Int32)reader[((int)AdjustmentDetailsColumn.FinalQuantity - 1)];
			entity.Remark = (reader.IsDBNull(((int)AdjustmentDetailsColumn.Remark - 1)))?null:(System.String)reader[((int)AdjustmentDetailsColumn.Remark - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Pharmacy.Entities.AdjustmentDetails"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.AdjustmentDetails"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Pharmacy.Entities.AdjustmentDetails entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.StockOutDetailId = (System.Int64)dataRow["StockOutDetailId"];
			entity.AdjustmentId = (System.String)dataRow["AdjustmentId"];
			entity.DrugId = (System.String)dataRow["DrugId"];
			entity.Quantity = (System.Int32)dataRow["Quantity"];
			entity.AdjustQuantity = (System.Int32)dataRow["AdjustQuantity"];
			entity.FinalQuantity = (System.Int32)dataRow["FinalQuantity"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Pharmacy.Entities.AdjustmentDetails"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Pharmacy.Entities.AdjustmentDetails Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, Pharmacy.Entities.AdjustmentDetails entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region AdjustmentIdSource	
			if (CanDeepLoad(entity, "Adjustment|AdjustmentIdSource", deepLoadType, innerList) 
				&& entity.AdjustmentIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.AdjustmentId;
				Adjustment tmpEntity = EntityManager.LocateEntity<Adjustment>(EntityLocator.ConstructKeyFromPkItems(typeof(Adjustment), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.AdjustmentIdSource = tmpEntity;
				else
					entity.AdjustmentIdSource = DataRepository.AdjustmentProvider.GetByAdjustmentId(transactionManager, entity.AdjustmentId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'AdjustmentIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.AdjustmentIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.AdjustmentProvider.DeepLoad(transactionManager, entity.AdjustmentIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion AdjustmentIdSource

			#region DrugIdSource	
			if (CanDeepLoad(entity, "DrugDispo|DrugIdSource", deepLoadType, innerList) 
				&& entity.DrugIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.DrugId;
				DrugDispo tmpEntity = EntityManager.LocateEntity<DrugDispo>(EntityLocator.ConstructKeyFromPkItems(typeof(DrugDispo), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DrugIdSource = tmpEntity;
				else
					entity.DrugIdSource = DataRepository.DrugDispoProvider.GetByDrugId(transactionManager, entity.DrugId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DrugIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DrugIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DrugDispoProvider.DeepLoad(transactionManager, entity.DrugIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DrugIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Pharmacy.Entities.AdjustmentDetails object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Pharmacy.Entities.AdjustmentDetails instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Pharmacy.Entities.AdjustmentDetails Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, Pharmacy.Entities.AdjustmentDetails entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region AdjustmentIdSource
			if (CanDeepSave(entity, "Adjustment|AdjustmentIdSource", deepSaveType, innerList) 
				&& entity.AdjustmentIdSource != null)
			{
				DataRepository.AdjustmentProvider.Save(transactionManager, entity.AdjustmentIdSource);
				entity.AdjustmentId = entity.AdjustmentIdSource.AdjustmentId;
			}
			#endregion 
			
			#region DrugIdSource
			if (CanDeepSave(entity, "DrugDispo|DrugIdSource", deepSaveType, innerList) 
				&& entity.DrugIdSource != null)
			{
				DataRepository.DrugDispoProvider.Save(transactionManager, entity.DrugIdSource);
				entity.DrugId = entity.DrugIdSource.DrugId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region AdjustmentDetailsChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Pharmacy.Entities.AdjustmentDetails</c>
	///</summary>
	public enum AdjustmentDetailsChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Adjustment</c> at AdjustmentIdSource
		///</summary>
		[ChildEntityType(typeof(Adjustment))]
		Adjustment,
			
		///<summary>
		/// Composite Property for <c>DrugDispo</c> at DrugIdSource
		///</summary>
		[ChildEntityType(typeof(DrugDispo))]
		DrugDispo,
		}
	
	#endregion AdjustmentDetailsChildEntityTypes
	
	#region AdjustmentDetailsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;AdjustmentDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AdjustmentDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class AdjustmentDetailsFilterBuilder : SqlFilterBuilder<AdjustmentDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsFilterBuilder class.
		/// </summary>
		public AdjustmentDetailsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public AdjustmentDetailsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public AdjustmentDetailsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion AdjustmentDetailsFilterBuilder
	
	#region AdjustmentDetailsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;AdjustmentDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AdjustmentDetails"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class AdjustmentDetailsParameterBuilder : ParameterizedSqlFilterBuilder<AdjustmentDetailsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsParameterBuilder class.
		/// </summary>
		public AdjustmentDetailsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public AdjustmentDetailsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public AdjustmentDetailsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion AdjustmentDetailsParameterBuilder
	
	#region AdjustmentDetailsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;AdjustmentDetailsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AdjustmentDetails"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class AdjustmentDetailsSortBuilder : SqlSortBuilder<AdjustmentDetailsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AdjustmentDetailsSqlSortBuilder class.
		/// </summary>
		public AdjustmentDetailsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion AdjustmentDetailsSortBuilder
	
} // end namespace
